package com.caishi.lkx.user.ienum.type;

import com.caishi.lkx.user.service.*;
import com.zzw.common.service.TypeEnum;

import lombok.Getter;

import java.util.Arrays;

/**
 * @author by keray
 * date:2019/8/3 14:30
 */
public enum UserType implements TypeEnum<IUserService<?>> {
    //
    //yktGroupMember("云课堂机构用户", 512, IMemberService.class),
    //yktMember("云课堂普通用户", 256, IMemberService.class),
    channelBusiness("渠道商", 128, IChannelBusinessService.class),
    agent("代理商", 64, IAgentUserService.class),
    //("vers7机构用户", 16, IMemberService.class),
    //groupAdmin("机构管理员", 8, IAdminUserService.class),
    member("用户", 2, IMemberService.class),
    admin("管理员", 1, IAdminUserService.class);

    @Getter
    final String desc;

    @Getter
    final Integer value;

    @Getter
    final Class<? extends IUserService<?>> serviceBeanClass;

    UserType(String desc, Integer value, Class<? extends IUserService<?>> serviceBean) {
        this.desc = desc;
        this.value = value;
        this.serviceBeanClass = serviceBean;
    }

    public static UserType int2Obj(int value) {
        return Arrays.stream(UserType.values())
                .filter(v -> v.value == value).findFirst().orElse(null);
    }

    public static int array2Int(UserType[] userTypes) {
        int result = 0;
        for (UserType l : userTypes) {
            result |= l.getValue();
        }
        return result;
    }

    public static UserType[] int2Array(int value) {
        return Arrays.stream(UserType.values())
                .filter(v -> (v.getValue() & value) == v.getValue()).toArray(UserType[]::new);
    }

    public static boolean intHaveType(int value, UserType level) {
        return (value & level.value) == level.value;
    }

    public static boolean intAllHaveType(int value, UserType[] levels) {
        return Arrays.stream(levels).allMatch(v -> (v.getValue() & value) == v.getValue());
    }

    public static boolean intAnyHave(int value, UserType[] levels) {
        return Arrays.stream(levels).anyMatch(v -> (v.getValue() & value) == v.getValue());
    }
}
